In software engineering, a spinlock is a lock where a thread simply waits in a loop (i.e., “spins”) repeatedly checking until the lock becomes available. As the thread remains active but isn't performing a useful task, the use of such a lock is a kind of busy waiting. Once acquired, spinlocks will usually be held until they are explicitly released, although in some implementations they may be automatically released if the thread being waited on (i.e., that which holds the lock) blocks, or goes to sleep.
Spinlocks are efficient if threads are only likely to be blocked for a short period of time, as they avoid overhead from operating system process re-scheduling or context switching. For this reason, spinlocks are often used inside operating system kernels. However, spinlocks become wasteful if held for longer durations, both preventing other threads from running and requiring re-scheduling. The longer a lock is held by a thread, the greater the risk that it will be interrupted by the operating system scheduler while holding the lock. If this happens, other threads will be left “spinning” (i.e., repeatedly trying to acquire the lock), while the thread holding the lock is not making progress towards releasing it. The result is an indefinite postponement until the thread holding the lock can finish and release it. This is especially true on a single-processor system, where each waiting thread of the same priority is likely to waste its quantum (allocated time where a thread can run) spinning until the thread that holds the lock is finally finished. Implementing spinlocks correctly is difficult because one must take into account the possibility of simultaneous access to the lock to prevent race conditions.
CPU caches for multiple core computers (e.g., multi-processor and multi-core computers) are often implemented using the MESI protocol or a variant thereof. Such protocols have the characteristic that if the same cache line is accessed for reading from one or multiple cores, or only read and written on a single core, the performance is good. But if the cache line is written on at least one core and read or written by other cores, the performance significantly degrades. Such degradation can be expected by design or can be due to false sharing. False sharing occurs when two logically separate memory locations are allocated the same cache line, and logically independent accesses from different cores suffer such performance degradation. Accordingly, a spinlock which is accessed from multiple cores will see the performance degradation due to multiple cores accessing the same cache line, as well as time spent doing a “busy wait”.
A known variant of the spinlock allows multiple readers and a single writer to use multiple bits in a byte, with a bit corresponding to each core. A core that wishes to be a writer must be able to set all of the bits associated with all cores; each core that wishes to only read need only set one bit. This variant allows readers to avoid “busy waiting” when other cores are only reading. It requires a “compare and exchange” or similar instruction in order to examine and change the values of all of the bits atomically.